@{
    ViewData["Title"] = "添加";
    Layout = "~/Areas/Manager/Views/Shared/_Layout.cshtml";
}
<div class="layui-bg">
    <form class="layui-form" style="padding: 20px 30px 0 0;" method="post">
        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-col-xs9">
                <select name="MsgType">
                    <option value="System">系统消息</option>
                    <option value="Order">订单消息</option>
                    <option value="Notice">通知消息</option>
                </select>
            </div>
            <div class="layui-tips-danger">
                *必选
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">接收者</label>
            <div class="layui-col-xs9">
                <textarea name="ReceiverIDStr" lay-verify="required" class="layui-textarea" style="height:150px"></textarea>
            </div>
            <div class="layui-tips-danger">
                （多个号码用一个空格隔开）
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-col-xs9">
                <input type="text" name="Title" lay-verify="required" class="layui-input">
            </div>
            <div class="layui-tips-danger">
                *必填
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">内容</label>
            <div class="layui-col-xs9">
                <textarea id="Content"  class="layui-textarea"  style="height:150px;width:100%" name="Content"></textarea>
            </div>
            <div class="layui-tips-danger">
                *必填
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-xs4"></div>
            <div class="layui-col-xs2">
                <button class="layui-btn" lay-submit lay-filter="add" id="submit" type="button">提交</button>
                <button id="close" type="button" class="layui-btn layui-btn-primary">关闭</button>
            </div>
        </div>
    </form>
</div>
@section scriptsfeet
    {
    <script type="text/javascript">
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径;
    }).use(['form','sykjwh'], function () {

        var $ = layui.$,form = layui.form,sykjwh = layui.sykjwh;

        $("#close").click(function () {
            sykjwh.close();
        });

        //表单提交
        form.on('submit(add)', function (obj) {
            var receiverUserNameList = [];
            //通过空格将字符串拆分成接收者用户名的数组
            var arr = obj.field.ReceiverIDStr.split(' ');
            //去重复
            for (var i = 0; i < arr.length; i++) {
                var items = arr[i];
                //判断元素是否存在于receiverUserNameList中，如果不存在则插入到receiverUserNameList的最后
                if ($.inArray(items, receiverUserNameList) == -1) {
                    receiverUserNameList.push(items);
                }
            }
            obj.field.ReceiverUserNameList = receiverUserNameList;
            console.log(obj.field.ReceiverUserNameList);
            save('@Url.Action("add", "Sitemessage", new { area = Constant.AREAMANAGER })', obj.field);
        });
        
        //自定义保存方法，将错误提示修改为弹出层，方便用户找到并修改错误的接收者
        function save(url, data, callback) {
            //loading层
            var loading2 = layer.load(1, {
                shade: [0.1, '#fff'] //0.1透明度的白色背景
            });

            //请求
            $.ajax({
                url: url,
                type: 'POST',
                data: data,
                success: function (res) {
                    layer.close(loading2);
                    if (res.success === true) {
                        //成功的提示
                        layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                            var llayer = window.parent.layer;
                            //关闭窗口
                            var index = parent.layer.getFrameIndex(window.name);
                            llayer.close(index);
                            //刷新父窗口表格
                            if (window.parent.layui.table) {
                                var loading = llayer.msg('数据请求中', { icon: 16, shade: 0.01, time: 0 });
                                window.parent.layui.table.reload('list', {
                                    done: function (res, curr, count) {
                                        llayer.close(loading);
                                    }
                                });
                            }
                        });
                    }
                    else {
                        layer.open({
                            title: '错误提示'
                            , content: res.msg
                        });
                    }
                },
                error: function (err) {
                    layer.msg(err, { icon: 2, time: 1000 });
                }
            });
        }
    });
    </script>
}
